﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RaProWorld
{
    class KomaZiegen : RaProGame
    {
        protected const int MAXGOATS = 30;
        protected const int MAXWAYPOINTS = 15;
        
        protected ContentManager ZiegenContent;
        protected RaProWorld game;

        protected MouseState mouseState;
        protected MouseState previousMouseState;
        protected Vector2 mousePos;

        protected KeyboardState keyboardState;
        protected KeyboardState previousKeyboardState;

        protected Texture2D background;
        protected Texture2D mouseCursor;
        protected Texture2D doseButton;

        protected KomaZiegen_objectMap objMap;
        protected List<KomaZiegen_goat> goats;
        protected List<Vector2> waypoints;
        protected List<Rectangle> toasts;

        protected SpriteFont gameFont;

        protected Rectangle shockButton;
        protected bool shockActive;

        protected Score ziegenScore;
        
        public KomaZiegen()
        {
            game = Program.raProWorld;
            ZiegenContent = new ContentManager(game.Services, "Content\\Games\\KomaZiegen");
            objMap = new KomaZiegen_objectMap(game.viewportRect.Width, game.viewportRect.Height, 10);
            ziegenScore = new Score(50,100,200);
            initialize();
            LoadContent(ZiegenContent);
        }
        
        #region RaProGame Members

        public void initialize()
        {
            mouseState = Mouse.GetState();
            keyboardState = Keyboard.GetState();

            goats = new List<KomaZiegen_goat>();
            waypoints = new List<Vector2>();
            toasts = new List<Rectangle>();
            shockButton = new Rectangle(950,690,50,50);
            shockActive = false;
        }

        public void LoadContent(ContentManager Content)
        {
            Random random = new Random();
            
            background = ZiegenContent.Load<Texture2D>("boden");
            mouseCursor = ZiegenContent.Load<Texture2D>("zeiger");
            gameFont = game.Content.Load<SpriteFont>("Fonts\\TextboxFont");
            doseButton = ZiegenContent.Load<Texture2D>("dose");

            for (int y = 10; y <= 750; y += 40)
            {
                for (int x = 10; x < 1000; x += 40)
                {
                    toasts.Add(new Rectangle(x, y, 30, 30));
                }
            }
            
            for (int i = 0; i < MAXWAYPOINTS; i++)
            {
                waypoints.Add(
                    new Vector2(
                        game.viewportRect.Width * 0.1f + MathHelper.Lerp(0.0f, game.viewportRect.Width * 0.8f, (float)random.NextDouble()),
                        game.viewportRect.Height * 0.1f + MathHelper.Lerp(0.0f, game.viewportRect.Height * 0.8f, (float)random.NextDouble())));
            }
        }

        public void Update(GameTime gameTime)
        {
            objMap.reset();
            objMap.update(toasts);
            objMap.update(goats);
            for (int i = 0; i < goats.Count; i++)
            {
                goats[i].Update(objMap, ziegenScore);
            }
        }

        public void HandleInput()
        {
            previousMouseState = mouseState;
            mouseState = Mouse.GetState();

            mousePos = new Vector2((float)mouseState.X,(float)mouseState.Y);

            previousKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();

            if (keyboardState.IsKeyDown(Keys.Escape) &&
                !previousKeyboardState.IsKeyDown(Keys.Escape))
                Exit();

            if (keyboardState.IsKeyDown(Keys.Space) &&
                !previousKeyboardState.IsKeyDown(Keys.Space) &&
                goats.Count < MAXGOATS)
            {
                goats.Add(
                    new KomaZiegen_goat(
                        new Vector2((float)game.viewportRect.Width / 2, (float)game.viewportRect.Height / 2),
                        ZiegenContent.Load<Texture2D>("ziege"),
                        goats, toasts, waypoints));
            }

            if (shockActive && mouseState.LeftButton == ButtonState.Pressed &&
                previousMouseState.LeftButton != ButtonState.Pressed)
            {
                for (int i = 0; i < goats.Count; i++)
                {
                    Vector2 targetVec = goats[i].Position - mousePos;
                    if (targetVec.Length() < 350)
                    {
                        goats[i].isShocked = true;
                        goats[i].shockedTarget = targetVec;
                    }
                }
                mouseCursor = ZiegenContent.Load<Texture2D>("zeiger");
                shockActive = false;
            }
            
            if (shockButton.Contains(new Point((int)mousePos.X, (int)mousePos.Y)) && !shockActive)
            {
                shockButton = new Rectangle(930, 670, 70, 70);
                if (mouseState.LeftButton == ButtonState.Pressed &&
                    previousMouseState.LeftButton != ButtonState.Pressed)
                {
                    mouseCursor = ZiegenContent.Load<Texture2D>("dose");
                    shockActive = true;
                }
            }
            else
            {
                shockButton = new Rectangle(950, 690, 50, 50);
            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            spriteBatch.Draw(background, game.viewportRect, Color.White);
            
            for (int i = 0; i < toasts.Count; i++)
            {
                spriteBatch.Draw(ZiegenContent.Load<Texture2D>("toastpflanze"), toasts[i], Color.White);
            }
            
            for (int i = 0; i < waypoints.Count; i++)
            {
                spriteBatch.DrawString(gameFont, i.ToString(), waypoints[i], Color.White);
            }
            
            for (int i = 0; i < goats.Count; i++)
            {
                goats[i].Draw(spriteBatch);
            }

            spriteBatch.Draw(doseButton, shockButton, Color.White);
            spriteBatch.DrawString(gameFont,"Score: "+ziegenScore.getScore().ToString(),new Vector2(10,10),Color.Yellow);

            spriteBatch.Draw(mouseCursor, mousePos, Color.White);
        }

        public void Exit()
        {
            game.gameShell.runningGame = null;
            game.GameState = "WORLDMAP";
            game.mainScore.addScore(ziegenScore.getScore());
            game.worldMap.objects[game.worldMap.currentSelectedIndex].score.addScore(ziegenScore.getScore());
        }

        #endregion
    }
}